<?php

namespace Maestros\Controller;

use Maestros\Controller\GenericController;

class PersonaController extends GenericController {

    public function __construct() {
        
    }

//Incompleto
    /*public function get($id) {
        /*
         * Datos del persona COMPLETO (con el usuario) //por ahora :)
         * FALTAN LAS DEMAS FORANEAS
         * Ej: http://localhost/UptaebBackend/public/maestros/persona/1
         * Rsp: {"success":true,"data":[{"id":1,"login":"07443224","password":"123","estado":1,"_grupo":{"id":1,"nombre":"Estudiante","detalle":"Estudiantes activos"}}],"total":1}
         
        $id = $this->params()->fromRoute('id');
        $output = $this->getPersonaService()->getRegistroCompleto($id);
        return $this->convertirArrayAJson($output);
    }*/

    /**
     * Return list of resources
     * @return mixed
     */
    public function getList($id = 1, $param1 = 1, $param2 = 1, $param3 = 1) {
        $id = $this->params()->fromRoute('id');
        $param1 = $this->params()->fromRoute('param1');
        // obtener el limite de registros desde la url
        switch ($id) {
            case (0):
                /*
                 * Datos de la persona sin los objetos foraneos
                 * Ej: http://localhost/UptaebBackend/public/maestros/persona/0/1
                 * Rsp: {"success":true,"data":[{"id":"1","cedula":"07443224","nombre1":"nom1","nombre2":"nom2","apellido1":"ape1","apellido2":"ape2","fechaNacimiento":null,"email":"email@correo.com","concejoComunalPertenece":null,"concejoComunalNombre":null,"concejoComunalCargo":null,"otroOrganismo":null,"estado":"activo","_sexo":"1","_estadoCivil":null,"_discapacidad":"1","_ocupacion":"1","_nacionalidad":"1","_usuario":"1"}],"total":1}
                 */
                $id = $this->params()->fromRoute('param1');
                $output = $this->getPersonaService()->getRegistro($id);
                break;
            case (1):
                /*
                 * Listado de persona segun cedula
                 * http://localhost/UptaebBackend/public/maestros/persona/@metodo/@cedula
                 * Ej: http://localhost/UptaebBackend/public/maestros/persona/1/07443224
                 */
                $cedula = $this->params()->fromRoute('param1');
                $output = $this->getPersonaService()->getRegistroSegunCedula($cedula);
                break;
            default:
                return $param1;
        }
        return $this->convertirArrayAJson($output);
    }

    /**
     * Create a new resource
     * @param  mixed $data
     * @return mixed
     */
    public function create($data) {
//        $data = $this->params()->fromPost();
//        // Guardar el registro
//        $persona_service = new Service\PersonaService($this->getServiceLocator()->get('Zend\Db\Adapter'));
//        $result = $persona_service->createRegistro($data);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return $response;
    }

    public function update($id, $data) {
//        //http://guzzle.readthedocs.org/en/latest/http-messages.html
//        $id = $this->params()->fromRoute('id');
//        $json = $this->get($id);
//        $a = $json->getBody();
//        echo $a;
//        $b = new JsonModel(array(
//            'data' => $this->get($id),
//        ));
//        echo $b;
//        echo "update ";
//        $estado = (string) $this->params()->fromRoute('param1');
//        $turno = (string) $this->params()->fromRoute('param2');
//        //$data = array('salumestado'=>$estado, 'turno'=>$turno);
//        $persona = new Persona($id, "Michell", "apellido", "sexo", "1", "ciudad", $estado, "direccion", "nacido", "05/09/1989", "lugarNacimiento", "telefono", "1", "ocupacion2", "turno2", "municipio", "2", "2");
//        // actualizar el registro
//        //echo $this->getServiceLocator()->get('Zend\Db\Adapter')->getDriver()->getDatabasePlatformName();
//        $result = $this->getPersonaService()->updateRegistro($persona);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return new JsonModel(array(
//            'data' => $this->get($id),
//        ));
//        //return $response;
    }

    public function replaceList($data) {
//        $id = $this->params()->fromRoute('param1');
//        echo "replaceList ";
//        $registro_json = \Zend\Json\Json::encode($id, true);
//        $response = $this->getResponseWithHeader();
//        $response->getHeaders()->addHeaders(array(
//            'Content-Type' => 'application/json',)
//        );
//        $response->setContent($registro_json);
//        return $response;
    }

    /**
     * Delete an existing resource
     * @param  mixed $id
     * @return mixed
     */
    public function delete($id) {
//        // obtener el id del registro
//        $id = $this->params()->fromRoute('param1');
//        // Eliminar el registro
//        $persona_service = Service\PersonaService::getInstance();
//        $result = $persona_service->deleteRegistro($id);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return $response;
    }

}
